package com.yiyw.herostory.handler;

import com.yiyw.herostory.bo.UserBo;
import com.yiyw.herostory.manage.ChannelGroupManage;
import com.yiyw.herostory.manage.UserManage;
import com.yiyw.herostory.proto.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;

/**
 * @Author yiyw
 * @Description 玩家移动事件
 * @Date 2022/8/14 14:53
 **/
public class UserMoveToCmdHandler implements ICmdHandler<GameMsgProtocol.UserMoveToCmd> {

    @Override
    public void handler(ChannelHandlerContext ctx, GameMsgProtocol.UserMoveToCmd message) {
        // 获取用户信息
        int id = ChannelGroupManage.getUserIdByChannel(ctx.channel());
        UserBo user = UserManage.getUser(id);
        if (user == null) {
            return;
        }
        // 返回移动事件
        GameMsgProtocol.UserMoveToResult.Builder result = GameMsgProtocol.UserMoveToResult.newBuilder();
        result.setMoveToPosX(message.getMoveToPosX());
        user.setMoveToPosX(message.getMoveToPosX());
        result.setMoveToPosY(message.getMoveToPosY());
        user.setMoveToPosY(message.getMoveToPosY());
        result.setMoveFromPosX(message.getMoveFromPosX());
        user.setMoveFromPosX(message.getMoveFromPosX());
        result.setMoveFromPosY(message.getMoveFromPosY());
        user.setMoveFromPosY(message.getMoveFromPosY());
        result.setMoveStartTime(System.currentTimeMillis());
        user.setMoveStartTime(System.currentTimeMillis());
        // 获取用户 ID
        result.setMoveUserId(id);

        UserManage.addUser(user);
        // 返回给所有的玩家
        ChannelGroupManage.writeAndFlush(result.build());
    }
}
